package com.zhoukun.cloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhoukun.cloud.CommentRest;
import com.zhoukun.cloud.pojo.tStorage;
import com.zhoukun.cloud.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author zhoukun 86547462@qq.com
 * @version 1.0
 * @date 2020/11/3 0:06
 */
@RestController
public class storageController {

    @Autowired
    StorageService service;
    @GetMapping("/storage/getall")
    public CommentRest getall()
    {
        return new CommentRest().setCode(200).setMessage("success").setData(service.list());

    }

    //减库存
    @GetMapping("/storage/up/{id}/{num}")
    public CommentRest up(@PathVariable Long id,@PathVariable Integer num) throws InterruptedException {
        tStorage storage=service.getById(id);


       if(storage.getResidue()<num)
       {
           return new CommentRest().setCode(400).setMessage("库存不足");
       }
       QueryWrapper<tStorage> wrapper=new QueryWrapper<tStorage>();
       wrapper.eq("product_id",id);
       service.update(storage.setUsed(storage.getUsed()+num).setResidue(storage.getResidue()-num),wrapper);
        return new CommentRest().setCode(200).setMessage("success");

    }


}
